cd "C:\Users\Shea\Dropbox\Slantchev Revised\Empirical Work\Data\Analysis"
use "wardefaultJCR.dta", clear

**Table 1 - Cross tabs with externel debt only
lab var edef2 "Default"
lab var win "War Outcome"
label define edef2 0 "No Default" 1 "Default"
label define win 0 "Lose" 1 "Win"

tab edef2 win , chi2 r co
tabout  edef2 win using table1.txt, replace  cells(freq col) layout(rb)  style(tex) ///
format(0 1)  

list ccode year if edef2==1 & win !=. 



**Table 3 - War Model 

*Selection stage (Table c11 in appendix)
probit data  irst_pc irst_gr lnupop wyrs  polity2 ukus data_region, cluster(ccode) 
capture drop p1 mills
predict p1, xb

estimates store sel2
gen mills = exp(-.5*p1^2)/(sqrt(2*_pi)*normprob(p1))

*m1
probit war l.edef_rate polity2 lncap   ///
  wyrs w2 w3  , cluster(ccode) robust 
estimates store out0

*m2
probit war l.edef_rate polity2 lncap   ///
  wyrs w2 w3  mills, cluster(ccode) robust 
estimates store out1

*m3
probit war l.edef_rate polity2 lncap   inflationcrisisbi lnupop ///
  ukus  wyrs w2 w3 rival issues numgps mills, cluster(ccode) robust 
estimates store out2

*m3 with no selection
probit war l.edef_rate polity2 lncap   inflationcrisisbi lnupop ///
  ukus  wyrs w2 w3 rival issues numgps , cluster(ccode) robust 
   

lab var lncap "Capabilities"
lab var mills "Mill's Inverse Ratio"




*************************************************
**Table 4  and 5 Pr(Default) and War 

*Estimate Pr(Default) and Pr(Default onset)

*m1 in Table G.15 in appendix
probit rexternal eyrs irst_pc irst_gr wyrs lnupop polity2  ///
  ukus  e2 e3  reg_* , cluster(ccode)
estimates store pred1
capture drop pred2 lpred2
predict pred2 

gen lpred2 =l.pred2

xtset ccode year

set seed 654321
*m4 in Table 3 (manusctipt)
bootstrap,   reps(500)   strata(ccode): probit war lpred2 polity2 lncap  inflationcrisisbi lnupop ///
  ukus  wyrs w2 w3 rival issues numgps mills
  
estimates store out5

btscs rextdummy year ccode, g(eeyrs)

gen ee2 = (eeyrs*eeyrs)/1000
gen ee3 = (eeyrs*eeyrs*eeyrs)/1000000



replace rextdummy=. if rextdummy==0&rexternal==1

*m2 in Table G.15 in appendix
probit rextdummy  eeyrs irst_pc irst_gr wyrs lnupop polity2   ///
  ukus  ee2 ee3  reg_* , cluster(ccode) 
estimates store pred2
capture drop pred1
 predict pred1
 
 gen lpred1 =l.pred1

*m4 in Table 4 (manusctipt) 
set seed 654321
bootstrap,  reps(500)  strata(ccode): probit war lpred1 lncap inflationcrisisbi lnupop  ///
 polity2 ukus   wyrs w2 w3 rival issues numgps mills
estimates store out6


*Table 4
 sum pred1
sum pred1 if wars2==0
sum pred1 if warneg1==1 

sum pred2
sum pred2 if wars2==0
sum pred2 if warneg1==1 



gen warpart = pred1 if warneg1==1
gen warabst = pred1 if wars2==0
gen warpart2 = pred2 if warneg1==1
gen warabst2 = pred2 if wars2==0
 
ttest warpart=warabst, unpaired unequal 
ttest warpart2=warabst2, unpaired unequal 

lab var lpred2 "Pr(Default)"
lab var lpred1 "Pr(Default Onset)"
lab var edef_rate2 "Default Rate (20 yrs)"
lab var lncap "Capabilities"


*Table 3
     estout out0 out1 out2 out5 out6  , cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) starlevels(* 0.05 ) ///
	eqlab("/lnsig2u" "ln($\sigma^2_u$)", none) ///
stats( ll aic N, fmt(%9.1f %9.1f %9.0f) labels("Log-Likelihood" "AIC") star)  style(tex) ///
legend label collabels(none) varlabels(_cons Constant) order(L.edef_rate  lpred2 lpred1 polity2 lncap wyrs  inflationcrisisbi lnupop ///
  ukus rival issues numgps mills) drop(w2 w3) ///
mlabels ("Model 1" "Model 2" "Model 3" "Model 4") ///
title("")




************************************************************************
**Table 5 - Mids and ICB Crises

use "wardefaultJCR.dta", clear


*m1 if Table 5
probit conflict l.edef_rate polity2 lncap inflationcrisisbi   lnupop  ///
  ukus  cyrs c2 c3 rival issues numgps
 estimates store mid1
 



capture drop p3
predict p3, xb
*capture drop mills 


capture drop mills2
 gen mills2 = exp(-.5*p3^2)/(sqrt(2*_pi)*normprob(p3))
 
*m2 if Table 5 
probit fatmid l.edef_rate polity2  inflationcrisisbi lnupop lncap mills2  ///
  ukus, cluster(ccode) robust
 estimates store mid2
 
 *m3 if Table 5
probit icbcrisis l.edef_rate polity2 lncap lnupop inflationcrisisbi  ///
  ukus  iyrs i2 i3 rival issues numgps, cluster(ccode)
 estimates store icb1
 
capture drop p1
predict p1, xb

capture drop mills2
gen mills2 = exp(-.5*p1^2)/(sqrt(2*_pi)*normprob(p1))

 *m4 if Table 5
 probit violent2 l.edef_rate polity2 lnupop inflationcrisisbi  lncap mills2  ///
  ukus , cluster(ccode) 
  estimates store icb2
  
lab var inflationcrisisbi "Inflation Crisis"
lab var edef_rate "Default Rate"
lab var eyrs "Years since Default"
lab var wyrs2 "Years  since War"
lab var cyrs "Years  since MID"
lab var iyrs "Years  since ICB Crisis"
lab var e2 "Years  since Default$^2$"
lab var e3 "Years  since Default$^3$"
lab var majpow "Major Power"
lab var lncap "Military Capabilties"
lab var lnupop "ln (Urban Population)"
lab var data_region "Regional Default"
lab var mills "Mill's Ratio"
lab var numgps "Number of Major Powers"
lab var rival "Rivals"
lab var issues "Disputes"

  
     estout   mid1 mid2 icb1 icb2, cells(b(star fmt(%9.3f)) se(par fmt(%9.2f))) starlevels( * 0.05 ) ///
	eqlab("/lnsig2u" "ln($\sigma^2_u$)", none) ///
stats( ll aic N, fmt(%9.1f %9.1f %9.0f) labels("Log-Likelihood" "AIC") star)  style(tex) ///
legend label collabels(none) varlabels(_cons Constant) order(L.edef_rate polity2 lncap) drop( c2 c3 i2 i3 mills2 ) ///
mlabels ("Model 1" "Model 2" "Model 3" "Model 4") ///
title()
